package qlsl.androiddesign.view.subview.activityview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import qlsl.androiddesign.activity.subactivity.RecruitActivity;
import qlsl.androiddesign.activity.subactivity.RecruitListActivity;
import qlsl.androiddesign.adapter.baseadapter.FragmentPagerAdapter;
import qlsl.androiddesign.adapter.subadapter.RecruitAdapter;
import qlsl.androiddesign.fragment.basefragment.BaseFragment;
import qlsl.androiddesign.fragment.subfragment.JobFragment;
import qlsl.androiddesign.fragment.subfragment.RecruitFragment;
import qlsl.androiddesign.listener.GuidePageChangeListener;
import qlsl.androiddesign.util.commonutil.Log;
import qlsl.androiddesign.view.baseview.FunctionView;
import qlsl.androiddesign.view.commonview.HorizontalScrollListView;
import qlsl.androiddesign.view.subview.fragmentview.JobView;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.qlsl.androiddesign.kuaiwa.R;

/**
 * 求职,招聘页<br/>
 * 需要传入的键：<br/>
 * 传入的值类型： <br/>
 * 传入的值含义：<br/>
 * 是否必传 ：
 */
public class RecruitView extends FunctionView<RecruitActivity> implements OnItemClickListener {

	private HorizontalScrollListView listView;

	private TextView[] tv_guides;

	private ImageView iv_cursor;

	private ViewPager viewPager;

	private List<BaseFragment> list;

	private int preIndex;

	public RecruitView(RecruitActivity activity) {
		super(activity);
		setContentView(R.layout.activity_recruit);
	}

	protected void initView(View view) {
		setTitle("求职招聘");
		setRightButtonText("发布");
		showRightButton();

		listView = findViewById(R.id.listView);
		tv_guides = new TextView[2];
		tv_guides[0] = (TextView) view.findViewById(R.id.tv_guide1);
		tv_guides[1] = (TextView) view.findViewById(R.id.tv_guide2);
		iv_cursor = (ImageView) view.findViewById(R.id.iv_cursor);
		viewPager = (ViewPager) view.findViewById(R.id.viewPager);
		viewPager.setOffscreenPageLimit(2);
	}

	protected void initData() {
		setListViewData();
		setCursorImageMatrix();
	}

	private void setCursorImageMatrix() {
		int bmpW = BitmapFactory.decodeResource(activity.getResources(), R.drawable.iv_cursor).getWidth();
		DisplayMetrics dm = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;
		int offset = (screenW / tv_guides.length - bmpW) / 2;
		Matrix matrix = new Matrix();
		matrix.postTranslate(offset, 0);
		iv_cursor.setImageMatrix(matrix);

		setViewPagerAdapter(offset, bmpW);
	}

	protected void setListViewData() {
		String[] names = new String[] { "武汉", "北京", "上海", "河北", "辽宁", "黑龙江" };
		List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
		for (int index = 0; index < names.length; index++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("text", names[index]);
			list.add(map);
		}
		RecruitAdapter adapter = new RecruitAdapter(activity, list);
		listView.setAdapter(adapter);
	}

	protected void initListener() {
		listView.setOnItemClickListener(this);

	}

	public <T> void showData(T... t) {

	}

	public <T> void showNoData(T... t) {

	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.tv_guide1:
			doClickGuideView(view);
			break;
		case R.id.tv_guide2:
			doClickGuideView(view);
			break;
		case R.id.btn_right:
			doClickBtnRightView(view);
			break;
		default:
			int index = viewPager.getCurrentItem();
			list.get(index).onClick(view);
			break;
		}
	}

	// 点击发布按钮
	private void doClickBtnRightView(View view) {
		activity.startActivity(RecruitListActivity.class);

	}

	private void setViewPagerAdapter(int offset, int bmpW) {
		list = new ArrayList<BaseFragment>();
		BaseFragment fragment1 = new JobFragment();
		BaseFragment fragment2 = new RecruitFragment();
		list.add(fragment1);
		list.add(fragment2);
		viewPager.setAdapter(new FragmentPagerAdapter(activity.getSupportFragmentManager(), list));
		viewPager.setCurrentItem(0);
		tv_guides[0].setActivated(true);
		viewPager.setOnPageChangeListener(new GuidePageChangeListener(tv_guides, iv_cursor, offset, bmpW));
	}

	private void doClickGuideView(View view) {
		for (int index = 0; index < tv_guides.length; index++) {
			if (view == tv_guides[index]) {
				viewPager.setCurrentItem(index);
				tv_guides[index].setActivated(true);
			} else {
				tv_guides[index].setActivated(false);
			}
		}
	}

	// 点击城市网络请求
	public void onItemClick(AdapterView<?> adapterView, View arg1, int arg2, long arg3) {
		String city = (String) ((RecruitAdapter) adapterView.getAdapter()).getItem(arg2).get("text");
		((JobView) list.get(0).getFunctionView()).queryJobList(1, city);
		((qlsl.androiddesign.view.subview.fragmentview.RecruitView) list.get(1).getFunctionView()).queryJobList(1,
				city);
	}

}
